/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.Objects;
import java.util.Optional;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.scene.CuccCustInfoDetailDTO;
import com.iwhalecloud.bss.kite.cucc.service.cache.CuccCustInfoDetailCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

/**
 * <Description> <br>
 *
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr <br>
 */
@Service
public class CompanyNameHandler extends AbstractAttrHandler {

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Resource
    private IQuerySceneInstService querySceneInstService;
    /**
     * Description: <br>
     *
     * @author tengg<br>
     * @taskId <br>
     * @param commonAttr <br>
     */
    @Override
    public void init(CommonAttrVO commonAttr) {
        CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        String sceneInstId = KiteWebContext.getContextNew().getCurrentSceneInstId();
        CuccCustInfoDetailDTO cuccCustInfoDTO = CuccCustInfoDetailCache.get(custDTO1.getCustId());
        if(Objects.nonNull(cuccCustInfoDTO)) {
            // 单位名称
            commonAttr.setValue(cuccCustInfoDTO.getCustName());
            commonAttr.setValueDesc(cuccCustInfoDTO.getCustName());
            // 证件类型
            KiteAttr cardTypeAttr = querySceneInstService.getCommonAttr(sceneInstId, "card_type");
            if (Objects.nonNull(cardTypeAttr)) {
                String desc = Optional.ofNullable(AttrValueCache.getAttrValue(cardTypeAttr.getAttrId(), cuccCustInfoDTO.getCertiTypeCode())).map(AttrValue::getAttrValueDesc).orElse("");
                operateSceneCommonService.setCommonAttrValue(sceneInstId, cardTypeAttr.getFieldName(), cuccCustInfoDTO.getCertiTypeCode(), desc);
            }

            // 证件号码
            KiteAttr cardNoAttr = querySceneInstService.getCommonAttr(sceneInstId, "card_no");
            if (Objects.nonNull(cardNoAttr)) {
                if(KiteStringUtils.isNotEmpty(cuccCustInfoDTO.getCertiCode())){
                    cardNoAttr.setValue(cuccCustInfoDTO.getCertiCode());
                    cardNoAttr.setValueDesc(cuccCustInfoDTO.getCertiCode());
                }
                operateSceneCommonService.setCommonAttr(sceneInstId, cardNoAttr);
            }

            // 主办单位所属行业分类
            KiteAttr CON0071Attr = querySceneInstService.getCommonAttr(sceneInstId, "CON0071");
            if (Objects.nonNull(CON0071Attr)) {
                String code = cuccCustInfoDTO.getFirstCallingTypeCode();
                CON0071Attr.setValue(code);
                CON0071Attr.setValueDesc(Optional.ofNullable(AttrValueCache.getAttrValue(CON0071Attr.getAttrId(), code)).map(AttrValue::getAttrValueDesc).orElse(""));
                operateSceneCommonService.setCommonAttr(sceneInstId, CON0071Attr);
            }

            // 联系人姓名
            KiteAttr contNameAttr = querySceneInstService.getCommonAttr(sceneInstId, "cont_name");
            if (Objects.nonNull(contNameAttr)) {
                contNameAttr.setValue(cuccCustInfoDTO.getContactName());
                operateSceneCommonService.setCommonAttr(sceneInstId, contNameAttr);
            }

            // 联系人电话
            KiteAttr contMobilAttr = querySceneInstService.getCommonAttr(sceneInstId, "cont_mobil");
            if (Objects.nonNull(contMobilAttr)) {
                contMobilAttr.setValue(cuccCustInfoDTO.getContactPhone());
                operateSceneCommonService.setCommonAttr(sceneInstId, contMobilAttr);
            }

            // 联系人电子邮件
            if(KiteStringUtils.isNotEmpty(cuccCustInfoDTO.getContactEmail())){
                KiteAttr contMailAttr = querySceneInstService.getCommonAttr(sceneInstId, "cont_mail");
                if (Objects.nonNull(contMailAttr)) {
                    operateSceneCommonService.setCommonAttrValue(sceneInstId, contMailAttr.getFieldName(), cuccCustInfoDTO.getContactEmail(), cuccCustInfoDTO.getContactEmail());
                }
            }
            setCommonAttr(commonAttr);
        }
    }

}
